package com.example.aliceapp;

import com.example.aliceapp.R;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.widget.FrameLayout;
import android.widget.TextView;

public class Accelerometer extends Activity implements SensorEventListener {

	private BluetoothConnector bc;
	private CrosshairCanvas c;
	Sensor accelerometer;
	SensorManager sm;
	TextView acceleration;
	TextView values;
	int x, y, z;
	int rotation = 0;
	float posX, posY;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.accelerometer);
		bc = BluetoothConnector.getInstance();
		sm = (SensorManager) getSystemService(SENSOR_SERVICE);
		accelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		sm.registerListener(this, accelerometer,
				SensorManager.SENSOR_DELAY_NORMAL);
		values = (TextView) findViewById(R.id.values);
		FrameLayout fLayout = (FrameLayout) findViewById(R.id.crosshair);
		c = new CrosshairCanvas(this);
		fLayout.addView(c);

	}
	@Override
	protected void onStop(){
		super.onStop();
		sm.unregisterListener(this);
		bc.stop();
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		// TODO Auto-generated method stub
		// acceleration.setText("X: " + event.values[0] + "\nY: " +
		// event.values[1] + "\nZ: " + event.values[2]);

		turnFloatsToInts(event);

		values.setText("X: " + x + "\n\nY: " + y + "\n\nZ: " + z);

		moveWithChars();
		
		c.setPos(event.values[0], event.values[1]);
		c.invalidate();
		
//		char direction = getDirectionChar();
//		char magnitudeRotationChar = getMagnitudeRotationChar();
//
//		bc.sendAccelerometerValues(direction, magnitudeRotationChar);
	}

	private char getMagnitudeRotationChar() {
		int pivot = (int) Math.pow(2, 3);
		int magnitude = (int) Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)) / 16;

		int rotationMagnitude = (char) magnitude + rotation * pivot;

		return (char) rotationMagnitude;
	}

	private char getDirectionChar() {
		int direction;
		if (y == 0) {
			if (x > 0) {
				direction = 15;
			} else if (x < 0) {
				direction = 45;
			} else {
				direction = 0;
			}
		} else if (x == 0) {
			if (y > 0) {
				direction = 0;
			} else if (y < 0) {
				direction = 30;
			} else {
				direction = 0;
			}
		} else {
			direction = (int) (360 - Math.atan(y / x) % 360) / 60;
		}
		if (direction == 60){
			direction = 0;
		}
		return (char) direction;
	}

	private void turnFloatsToInts(SensorEvent event) {
		// Float to Int
		x = (int) event.values[0];
		y = (int) event.values[1];
		z = (int) event.values[2];
	}

	private void moveWithChars() {
		if (y <= -2 && x <= -2) {
			bc.moveNorthWest();
		}

		else if (y >= 2 && x <= -2) {
			bc.moveNorthEast();
		}

		else if (y >= 2 && x >= 2) {
			bc.moveSouthEast();
		}

		else if (y <= -2 && x >= 2) {
			bc.moveSouthWest();
		}

		else if (x >= 2) {
			bc.moveBackward();
		}

		else if (x <= -2) {
			bc.moveForward();
		}

		else if (y >= 2) {
			bc.moveRight();
		} else if (y <= -2) {
			bc.moveLeft();
		}

		else {
			bc.stop();
		}
	}

}
